﻿using System;
using System.Data;
using System.IO;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class CopyFiles : MissionPlugin
    {
        [Package(Direction.Input, "Defines the files, you like to copy.")]
        enum SourceFileNames
        {
            [Column(typeof(string), "The file you like to copy?",
                Validation = "isexistingfile")]
            Filename
        }

        [Package(Direction.Input, "Defines the directories, where you like to copy.")]
        enum DestinationPath
        {
            [Column(typeof(string), "The directory, where you like to copy the files.",
                Validation = "ispath")]
            Directory,
            [Column(typeof(bool), "Should an existing destination file overwritten?",
                DefaultValue = "false")]
            Override
        }

        [Plugin(PluginDevStatus.Production, "Copies the given files to the expected destination directories.")]
        public CopyFiles()
        {
        }

        public override void Initialize()
        {

            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        /// <summary>
        /// Generate new Source-content.
        /// </summary>
        protected override void Work()
        {
            DataTableReader copyFiles = this.GetDataReader(typeof(SourceFileNames));
            while (copyFiles.Read())
            {
                FileInfo fileToCopyOn = new FileInfo(copyFiles[SourceFileNames.Filename.ToString()].ToString());

                DataTableReader destinationPaths = this.GetDataReader(typeof(DestinationPath));
                while (destinationPaths.Read())
                {
                    string destinationPathOn = destinationPaths[DestinationPath.Directory.ToString()].ToString();
                    this.AddNewLoggingMessage(typeof(SourceFileNames), String.Format("copy file {0} to:{1}", fileToCopyOn.FullName, destinationPathOn));

                    // Create the subfolder, if not existing
                    if (false == Directory.Exists(destinationPathOn))
                    {
                        System.IO.Directory.CreateDirectory(destinationPathOn);
                    }

                    File.Copy(fileToCopyOn.FullName, destinationPathOn + "\\" + fileToCopyOn.Name, Convert.ToBoolean(destinationPaths[DestinationPath.Override.ToString()]));
                }
            }
        }
    }
}
